import 'dart:async';

import 'package:flutter_ytx_plugin/x5_request.dart';

class X5Plugin implements X5Response {
  // X5Plugin._privateConstructor(){
  //   print("X5Sdk:初始化X5内核");
  //   X5Response.setup(this);
  //   _x5Request?.initX5Sdk();
  // }

  // static final X5Plugin _instance = X5Plugin._privateConstructor();
  //
  // static X5Plugin get instance => _instance;

  // 使用懒加载确保只有在需要的时候才创建 X5Request 实例
  X5Plugin() {
    X5Response.setup(this);
    _initializeX5Request();
  }

  static X5Request? _x5Request;

  X5Request get x5request {
    _x5Request ??= _initializeX5Request();
    return _x5Request!;
  }

  bool _isX5RequestInitialized = false;

  X5Request _initializeX5Request() {
    if (!_isX5RequestInitialized) {
      _x5Request = X5Request();
      _isX5RequestInitialized = true;
      _x5Request?.initX5Sdk();
    }
    return _x5Request!;
  }

  Future<void> openWebActivity(Map<String?, Object?> map) async {
    x5request.openWebActivity(map);
  }

  final StreamController<Map<String, dynamic>> _repController =
      StreamController.broadcast();

  Stream<Map<String, dynamic>> get response => _repController.stream;

  @override
  void onCoreInitFinished(int p0) {
    _repController.add({"key": "onCoreInitFinished", "value": "$p0"});
    //print("X5Sdk: onInstallFinish:$p0");
  }

  @override
  void onDownloadFinish(int p0) {
    print("X5Sdk: onDownloadFinish:$p0");
    _repController.add({"key": "onDownloadFinish", "value": "$p0"});
  }

  @override
  void onDownloadProgress(int p0) {
    print("X5Sdk: onDownloadProgress:$p0");
    _repController.add({"key": "onDownloadProgress", "value": "$p0"});
  }

  @override
  void onHideCustomView() {
    // print("X5Sdk: onHideCustomView:");
    _repController.add({"key": "onHideCustomView", "value": ""});
  }

  @override
  void onInstallFinish(int p0) {
    // print("X5Sdk: onInstallFinish:$p0");

    _repController.add({"key": "onInstallFinish", "value": ""});
  }

  @override
  void onJavascriptInterface(Map<String?, Object?> map) {
    // print("X5Sdk: onJavascriptInterface:$map");
    _repController.add({"key": "onJavascriptInterface", "value": map});
  }

  @override
  void onPageStarted(Map<String?, Object?> map) {
    _repController.add({"key": "onPageStarted", "value": map});
  }

  @override
  void onPageFinished(Map<String?, Object?> map) {
    // print("X5Sdk: onPageFinished:$map");
    _repController.add({"key": "onPageFinished", "value": map});
  }

  @override
  void onPlatformViewCreated(int p0) {
    // print("X5Sdk: onPlatformViewCreated:$p0");
  }

  @override
  void onProgressChanged(Map<String?, Object?> map) {
    print("X5Sdk: onProgressChanged:$map");
    int progress = map['progress'] as int? ?? 0;
    _repController.add({"key": "onProgressChanged", "value": progress});
  }

  @override
  void onShowCustomView() {
    // print("X5Sdk: onShowCustomView:");
    _repController.add({"key": "onShowCustomView", "value": ""});
  }

  @override
  void onUrlLoading(Map<String?, Object?> map) {
    // print("X5Sdk: onUrlLoading:$map");
    _repController.add({"key": "onShowCustomView", "value": map});
  }

  @override
  void onViewInitFinished(int p0) {
    print("X5Sdk: onViewInitFinished:$p0");
  }

  dispose() {
    _repController.close();
  }
}
